package com.huike.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huike.excel.DoctorExcel;
import com.huike.excel.HospitalManagementExcel;
import com.huike.pojo.Doctor;
import com.huike.pojo.Register;
import com.huike.service.DoctorService;
import com.huike.vo.ExportExcel;
import com.huike.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping(value = "Doctor")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;
//查询所有医生
    @RequestMapping(value = "findDoctorAll")
    public ResultVO findDoctorAll() {
        List<Doctor> doctors = doctorService.findDoctorAll();
        return new ResultVO(doctors);
    }
//根据ID查询医生
    @RequestMapping(value = "findDoctorById")
    public ResultVO findDoctorById(Integer id) {
        Doctor doctors = doctorService.findDoctorById(id);
        return new ResultVO(doctors);
    }
//更新医生信息
    @RequestMapping(value = "updateForDoctor" )
    public ResultVO updateForDoctor( Doctor doctor) {
        doctorService.updateForDoctor(doctor);
        return new ResultVO();
    }
//分页查询所有医生
    @RequestMapping(value = "findDoctorAllPagination")
    public ResultVO findDoctorAllPagination(Integer id, String doctorName, String deptName,
                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Doctor> doctors = doctorService.findDoctorAllPagination(id, doctorName, deptName);
        PageInfo pageInfo = new PageInfo(doctors, 5);
        return new ResultVO(pageInfo);
    }
    //添加医生
    @RequestMapping(value = "doAdd", method = RequestMethod.POST)
    public ResultVO add(Doctor doctor){
        doctorService.addDoctor(doctor);
        return new ResultVO();
    }
    //医生Excel导出
    @RequestMapping("excelInfo")
    public ResultVO<Void> excelInfo(String outStream,Integer doctorId, String doctorName, String deptName){
        List<Doctor>  list = doctorService.findDoctorAllPagination(doctorId,doctorName,deptName);
        List<DoctorExcel> doctorExcels = new ArrayList<>();//存储想要字段
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for(int i = 0;i< list.size();i++){
            DoctorExcel doctorExcel1 = new DoctorExcel();
            Doctor doctor = list.get(i);
           doctorExcel1.setId(doctor.getId());
           doctorExcel1.setDoctorName(doctor.getDoctorName());
           doctorExcel1.setInTime(doctor.getInTime() == null?"未登记时间": sdf.format(doctor.getInTime()));
           doctorExcel1.setDeptName(doctor.getDepartment()==0?"未分配科室":doctor.getDept().getDeptName());
            doctorExcels.add(doctorExcel1);
        }
        String[] headerStr = {"医生编号","医生姓名","入院时间","所属科室"};//表头
        String fileName=outStream +"\\"+ sdf.format(new Date())+"医生表.xlsx";//生成文件名称
        ExportExcel exportExcel = new ExportExcel();
        try{
            OutputStream outputStream = new FileOutputStream(fileName);
            exportExcel.exportExcel(doctorExcels,outputStream,headerStr);
            outputStream.close();
        }catch (Exception e){
            return new ResultVO<>(500,"导出时未知异常");
        }
        return new ResultVO<>(200,"导出成功");
    }

    @RequestMapping("getDoctorByDept")
    public List<Doctor> getDoctorByDept(Integer department){
        return doctorService.getDoctorByDept(department);
    }

}
